<template>
  <div class="login">
    <div class="container">
      <h1>后台管理</h1>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        class="login-form"
        status-icon
      >
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="form.username"
            placeholder="账户"
            prefix-icon="myicon myicon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            placeholder="密码"
            prefix-icon="myicon myicon-key"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            @click="submitForm('form')"
            >提交</el-button
          >
          <el-button class="reset-btn" @click="resetForm('form')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {getLogin} from '@/api/index'


export default {
  data() {
    var validatePwd = (rule, value, callback) => {
      if (!value) return callback(new Error("密码不能为空"));
      callback()
    };
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
          { validator: validatePwd, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async submitForm(formName) {
        try {
          let success = await this.$refs[formName].validate();
          if(!success) return false;
          let {data:{username,token},meta:{status,msg}}= await getLogin(this.form);
          if(status == 200){
            this.$message.success(msg);
            this.$store.commit('setToken',token);
            this.$store.commit('setUserName',username);
            this.$router.push({path: '/home'});
          }
        } catch (error) {
          console.log(error)
        }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background-color: #2c3e50;
  .container {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    width: 300px;
    height: 450px;
    background-color: #34495e;
    border-radius: 20px;
    box-shadow: 15px 15px 10px rgba(33, 45, 58, 0.3);
    overflow: hidden;
    h1 {
      color: #ecf0f1;
      font-size: 36px;
      margin-bottom: 28px;
    }
    .login-form{
      .el-form-item{
        .login-btn,
        .reset-btn{
          margin: 0;
          margin-bottom: 10px;
          width: 100%;
        }
      }
    }
  }
}
</style>